* Computing number of elections for which data on outcomes is available on the year of the election

use "$project_path/data/3_cleaned/parliamentary_elections.dta", clear
keep Country Year Month Type_Election
append using "$project_path/data/3_cleaned/presidential_elections.dta"
keep Country Year Month Type_Election
merge m:1 Country Year using "$project_path/data/3_cleaned/outcomes", keep(master match) nogen

count if growth_gdp_penn!=.
count if inflation_imf!=.
count if unemployment_ilo!=.
count if trade_wb!=.
count if hdi!=.
count if democracy_index!=.

* Export table
//Initialize matrix to store stats
local ncols = 1
local nrows = 6

mat Stats = J(`nrows', `ncols', .)

local varlist growth_gdp_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index
	
local row = 0	
foreach var in `varlist'{
	
	local ++row
	count if `var' != .
	
	mat Stats[`row', 1] = r(N)
	
}

//Import matrix as dta and edit to preapare the table 
clear 
svmat Stats

rename Stats1 nb_elections
local categories category description more_is source time_coverage winsor
foreach var in `categories'{
	gen `var' = ""
}
order nb_elections, after(source)

* Fill in the values manually
replace description = "GDP per capita growth" in 1
replace category = "Economic" in 1
replace more_is = "Positive" in 1
replace source = "Penn World Tables" in 1
replace time_coverage = "1951-2014" in 1
replace winsor = "Yes" in 1

replace description = "Inflation" in 2
replace category = "Economic" in 2
replace more_is = "Negative" in 2
replace source = "IMF" in 2
replace time_coverage = "1945-2020" in 2
replace winsor = "Yes" in 2

replace description = "Unemployment" in 3
replace category = "Economic" in 3
replace more_is = "Negative" in 3
replace source = "ILO" in 3
replace time_coverage = "1991-2022" in 3
replace winsor = "Yes" in 3

replace description = "Trade intensity" in 4
replace category = "Economic" in 4
replace more_is = "Positive" in 4
replace source = "World Bank" in 4
replace time_coverage = "1960-2020" in 4
replace winsor = "Yes" in 4

replace description = "HDI" in 5
replace category = "Social" in 5
replace more_is = "Positive" in 5
replace source = "UNDP" in 5
replace time_coverage = "1990-2019" in 5
replace winsor = "No" in 5

replace description = "Democracy index" in 6
replace category = "Democracy" in 6
replace more_is = "Positive" in 6
replace source = "V-Dem" in 6
replace time_coverage = "1945-2020" in 6
replace winsor = "No" in 6

//Label vars
lab var category "Category" 
lab var description "Variable" 
lab var more_is "More is..." 
lab var source "Source" 
lab var time_coverage "Coverage" 
lab var winsor "Winsor."
lab var nb_elections "N"


local output "$project_path/output/appendix_tables/Appendix_Table_B1.tex"
local title = "Outcome variables"
local footnote = "\emph{Notes}: This table lists the variables which we use to measure country performance. Winsor. indicates whether the outcome variables were winsorized or not. N is the number of elections for which we have available data."

texsave using "`output'", replace title("`title'") varlabels footnote("`footnote'") frag marker("tab:des_stat_indices") align(p{1.8cm} p{4.8cm} p{1.7cm} p{3.5cm} p{0.7cm} p{1.9cm} p{1cm}) nofix location(h)

